﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_Model;
using V5_Common;
using System.Data;
using V5_DAL.SqlServer;

namespace V5_BLL {
    public class BLLProductOrder {
        DALProductOrder dal = new DALProductOrder();
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int OrderID) {
            return dal.Exists(OrderID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelProductOrder model) {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelProductOrder model) {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int OrderID) {

            dal.Delete(OrderID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductOrder GetModel(int OrderID) {

            return dal.GetModel(OrderID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public ModelProductOrder GetModelByCache(int OrderID) {

            string CacheKey = "ModelProductOrderModel-" + OrderID;
            object objModel = DataCacheHelper.GetCache(CacheKey);
            if (objModel == null) {
                try {
                    objModel = dal.GetModel(OrderID);
                    if (objModel != null) {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCacheHelper.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (ModelProductOrder)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelProductOrder> GetModelList(string strWhere) {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelProductOrder> DataTableToList(DataTable dt) {
            List<ModelProductOrder> modelList = new List<ModelProductOrder>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0) {
                ModelProductOrder model;
                for (int n = 0; n < rowsCount; n++) {
                    model = new ModelProductOrder();
                    if (dt.Rows[n]["OrderID"].ToString() != "") {
                        model.OrderID = int.Parse(dt.Rows[n]["OrderID"].ToString());
                    }
                    if (dt.Rows[n]["MemberID"].ToString() != "") {
                        model.MemberID = int.Parse(dt.Rows[n]["MemberID"].ToString());
                    }
                    if (dt.Rows[n]["OrderTime"].ToString() != "") {
                        model.OrderTime = DateTime.Parse(dt.Rows[n]["OrderTime"].ToString());
                    }
                    model.ShipTime = dt.Rows[n]["ShipTime"].ToString();
                    model.BuyerName = dt.Rows[n]["BuyerName"].ToString();
                    model.BuyerPhone = dt.Rows[n]["BuyerPhone"].ToString();
                    model.BuyerEmail = dt.Rows[n]["BuyerEmail"].ToString();
                    model.BuyerAddress = dt.Rows[n]["BuyerAddress"].ToString();
                    model.BuyerPostalcode = dt.Rows[n]["BuyerPostalcode"].ToString();
                    model.ReceiverName = dt.Rows[n]["ReceiverName"].ToString();
                    model.ReceiverPhone = dt.Rows[n]["ReceiverPhone"].ToString();
                    model.ReceiverEmail = dt.Rows[n]["ReceiverEmail"].ToString();
                    model.ReceiverProCities = dt.Rows[n]["ReceiverProCities"].ToString();
                    model.ReceiverAddress = dt.Rows[n]["ReceiverAddress"].ToString();
                    model.ReceiverPostalcode = dt.Rows[n]["ReceiverPostalcode"].ToString();
                    model.Remark = dt.Rows[n]["Remark"].ToString();
                    if (dt.Rows[n]["TotalPrice"].ToString() != "") {
                        model.TotalPrice = decimal.Parse(dt.Rows[n]["TotalPrice"].ToString());
                    }
                    model.ProductIDs = dt.Rows[n]["ProductIDs"].ToString();
                    if (dt.Rows[n]["ProductPrice"].ToString() != "") {
                        model.ProductPrice = decimal.Parse(dt.Rows[n]["ProductPrice"].ToString());
                    }
                    if (dt.Rows[n]["CustomItemPrice"].ToString() != "") {
                        model.CustomItemPrice = decimal.Parse(dt.Rows[n]["CustomItemPrice"].ToString());
                    }
                    model.Discount = dt.Rows[n]["Discount"].ToString();
                    if (dt.Rows[n]["ShipPrice"].ToString() != "") {
                        model.ShipPrice = decimal.Parse(dt.Rows[n]["ShipPrice"].ToString());
                    }
                    if (dt.Rows[n]["AdvancePayment"].ToString() != "") {
                        model.AdvancePayment = decimal.Parse(dt.Rows[n]["AdvancePayment"].ToString());
                    }
                    if (dt.Rows[n]["PaymentType"].ToString() != "") {
                        model.PaymentType = int.Parse(dt.Rows[n]["PaymentType"].ToString());
                    }
                    if (dt.Rows[n]["ShippingType"].ToString() != "") {
                        model.ShippingType = int.Parse(dt.Rows[n]["ShippingType"].ToString());
                    }
                    if (dt.Rows[n]["Score"].ToString() != "") {
                        model.Score = int.Parse(dt.Rows[n]["Score"].ToString());
                    }
                    if (dt.Rows[n]["Weight"].ToString() != "") {
                        model.Weight = decimal.Parse(dt.Rows[n]["Weight"].ToString());
                    }
                    if (dt.Rows[n]["Quantity"].ToString() != "") {
                        model.Quantity = int.Parse(dt.Rows[n]["Quantity"].ToString());
                    }
                    if (dt.Rows[n]["NeedInvoice"].ToString() != "") {
                        model.NeedInvoice = int.Parse(dt.Rows[n]["NeedInvoice"].ToString());
                    }
                    model.InvoiceHead = dt.Rows[n]["InvoiceHead"].ToString();
                    model.InvoiceContent = dt.Rows[n]["InvoiceContent"].ToString();
                    if (dt.Rows[n]["IsCancelled"].ToString() != "") {
                        model.IsCancelled = int.Parse(dt.Rows[n]["IsCancelled"].ToString());
                    }
                    if (dt.Rows[n]["IsConfirmed"].ToString() != "") {
                        model.IsConfirmed = int.Parse(dt.Rows[n]["IsConfirmed"].ToString());
                    }
                    if (dt.Rows[n]["PayStatus"].ToString() != "") {
                        model.PayStatus = int.Parse(dt.Rows[n]["PayStatus"].ToString());
                    }
                    if (dt.Rows[n]["ShippingStatus"].ToString() != "") {
                        model.ShippingStatus = int.Parse(dt.Rows[n]["ShippingStatus"].ToString());
                    }
                    if (dt.Rows[n]["IsFinished"].ToString() != "") {
                        model.IsFinished = int.Parse(dt.Rows[n]["IsFinished"].ToString());
                    }
                    model.ManageHistory = dt.Rows[n]["ManageHistory"].ToString();
                    if (dt.Rows[n]["LastModify"].ToString() != "") {
                        model.LastModify = int.Parse(dt.Rows[n]["LastModify"].ToString());
                    }
                    model.OrderNo = dt.Rows[n]["OrderNo"].ToString();
                    model.Code = dt.Rows[n]["Code"].ToString();
                    if (dt.Rows[n]["IsAgentF"].ToString() != "") {
                        model.IsAgentF = int.Parse(dt.Rows[n]["IsAgentF"].ToString());
                    }
                    model.IsAgentFContent = dt.Rows[n]["IsAgentFContent"].ToString();
                    if (dt.Rows[n]["IsShippingFree"].ToString() != "") {
                        model.IsShippingFree = int.Parse(dt.Rows[n]["IsShippingFree"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList() {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return dal.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        #endregion  成员方法

        /// <summary>
        /// 生成订单号
        /// </summary>
        /// <returns></returns>
        public string ProductOrderNum() {
            return DateTime.Now.ToString("yyMMddhhmmss");
        }

        /// <summary>
        /// 获取支付方式
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public string Get_Payment(object osid) {
            int sid = InputHelper.SetNumber(osid);
            string tempStr = "";
            switch (sid) {
                case 1:
                    tempStr = "货到付款";
                    break;
                case 2:
                    tempStr = "在线支付-快付宝";
                    break;
                case 3:
                    tempStr = "在线支付-支付宝";
                    break;
                case 4:
                    tempStr = "在线支付-快钱支付";
                    break;
                case 5:
                    tempStr = "在线支付-工行支付";
                    break;
                case 6:
                    tempStr = "邮政汇款";
                    break;
                default:
                    break;
            }
            return tempStr;
        }

        /// <summary>
        /// 获取送货地址
        /// </summary>
        /// <param name="osid"></param>
        /// <returns></returns>
        public string Get_Sendment(object osid) {
            string tempStr = "";
            int sid = InputHelper.SetNumber(osid);
            switch (sid) {
                case 1:
                    tempStr = "工作日、双休日与节假日均可送货";
                    break;
                case 2:
                    tempStr = "只有双休日、假日送货（工作日不用送货）";
                    break;
                case 3:
                    tempStr = "只有工作日送货（双休日、假日不用送）";
                    break;
                case 4:
                    tempStr = "学校地址（该地址白天没人，请尽量安排其他时间送货）";
                    break;
                default:
                    break;
            }
            return tempStr;
        }
    }
}
